package com.snopy.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration;
import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.route.strategy.type.hint.HintShardingStrategy;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DataSourceConfig
 * @description
 * @email 77912204@qq.com
 * @date 2022/03/21 15:34
 */
public class DataSourceConfig {
    public DataSource getDataSource(){
        String schemaName = "foo_schema";//指定逻辑 Schema 名称
        ModeConfiguration modeConfig = new ModeConfiguration("Memory", new StandalonePersistRepositoryConfiguration("Memory",new Properties()),false);//构建运行模式
        Map<String,DataSource> dataSourceMap = new HashMap<>();
        HikariDataSource dataSource1 = new HikariDataSource();
        dataSource1.setDriverClassName("com.mysql.hdbc.Driver");
        dataSource1.setJdbcUrl("jdbc:mysql://192.168.164.101:3306/snopy_101");
        dataSource1.setUsername("snopy");
        dataSource1.setPassword("12#$QWer");
        dataSourceMap.put("ds_1",dataSource1);
        HikariDataSource dataSource2 = new HikariDataSource();
        dataSource2.setDriverClassName("com.mysql.hdbc.Driver");
        dataSource2.setJdbcUrl("jdbc:mysql://192.168.164.102:3306/snopy_101");
        dataSource2.setUsername("snopy");
        dataSource2.setPassword("12#$QWer");
        dataSourceMap.put("ds_2",dataSource2);
        Collection<RuleConfiguration> ruleConfigs = new ArrayList<>();
        RuleConfiguration tablesRule = new ShardingRuleConfiguration();
        ShardingTableRuleConfiguration tableRuleConfiguration = new ShardingTableRuleConfiguration("order");
        ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
        HintShardingStrategyConfiguration hintShardingStrategyConfiguration = new HintShardingStrategyConfiguration("Mod");
        tableRuleConfiguration.setDatabaseShardingStrategy(()->{
            return String.valueOf(new ShardingRuleConfiguration());
        });
        Properties props = new Properties();
        DataSource dataSource = null;
        try {
            dataSource = ShardingSphereDataSourceFactory.createDataSource(schemaName,modeConfig,dataSourceMap,ruleConfigs,props);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
